home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Alert.C < prev    next >
C/C++ Source or Header  |  1992-08-24  |  4KB  |  206 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "Alert.h"
  6.  
  7. #include "Class.h"
  8. #include "Env.h"
  9. #include "ImageItem.h"
  10. #include "Buttons.h"
  11. #include "CheapText.h"
  12. #include "StaticTView.h"
  13. #include "StyledText.h"
  14. #include "OrdColl.h"
  15. #include "ObjArray.h"
  16. #include "Window.h"
  17. #include "Expander.h"
  18. #include "BorderItems.h"
  19.  
  20. static u_short NoteBits[]= {
  21. #   include "images/note.image"
  22. };
  23. static u_short CautionBits[]= {
  24. #   include "images/caution.image"
  25. };
  26. static u_short StopBits[]= {
  27. #   include "images/stop.image"
  28. };
  29. static u_short SunBits[]= {
  30. #   include "images/sun.image"
  31. };
  32. static u_short ErrorBits[]= {
  33. #   include "images/error.image"
  34. };
  35.  
  36. SmartBitmap gNote(Point(64, 52), NoteBits),
  37.         gCaution(Point(64, 52), CautionBits),
  38.         gStop(64, StopBits),
  39.         gSun("sun.xpm"), //gSun(64, SunBits),
  40.         gError(64, ErrorBits);
  41.  
  42. ONEXIT(Alert)
  43. {
  44.     if (Alert::alerts) {
  45.     Alert::alerts->FreeAll();
  46.     SafeDelete(Alert::alerts);
  47.     }
  48. }
  49.  
  50. //---- Alert -------------------------------------------------------------------
  51.  
  52. NewMetaImpl(Alert,Dialog, (TP(text), TP(textview), TP(image), TP(buttons), TP(alerts)));
  53.  
  54. ObjArray *Alert::alerts;
  55.  
  56. Alert::Alert(AlertType at, byte *message, Bitmap *va_(bm), ...) : Dialog()
  57. {
  58.     va_list ap;
  59.     OrdCollection *ol;
  60.     va_start(ap,va_(bm));
  61.  
  62.     if (message) {
  63.     text= new CheapText(message);
  64.     textview= new StaticTextView((View*)0,
  65.                     Rectangle(gSysFont->Width('n')*40, cFit), text);
  66.     
  67.     if (va_(bm))
  68.         image= new ImageItem(va_(bm), 0);
  69.     
  70.     if (at == eAlertMessage)
  71.         image= textview;
  72.     else
  73.         image= new HBox(gPoint10, eVObjVTop, image, textview, 0);
  74.     
  75.     ol= new OrdCollection;
  76.     char *s;
  77.     for (int i= 0; s= va_arg(ap, char*); i++)
  78.         ol->Add(new ActionButton(va_arg(ap, int), s, i == 0));   
  79.     buttons= new HBox(20, (VObjAlign)(eVObjVBase|eVObjHEqual|eVObjHExpand), ol);
  80.     }
  81.     va_end(ap);
  82. }
  83.  
  84. Alert::~Alert()
  85. {
  86.     SafeDelete(text);
  87. }
  88.  
  89. VObject *Alert::DoMakeContent()
  90. {
  91.     return new Matte(new VBox(20, eVObjHLeft, image, buttons, 0));
  92. }
  93.  
  94. int Alert::Show(char* va_(fmt), ...)
  95. {
  96.     int code;
  97.     va_list ap;
  98.     
  99.     va_start(ap,va_(fmt));
  100.     code= ShowV(va_(fmt), ap);
  101.     va_end(ap);
  102.     return code;
  103. }
  104.  
  105. int Alert::ShowV(char *fmt, va_list ap)
  106. {
  107.     return ShowV(gSysFont, fmt, ap);
  108. }
  109.  
  110. int Alert::ShowV(Font *fp, char *fmt, va_list ap)
  111. {
  112.     char *buf= strvprintf(fmt, ap);
  113.     SafeDelete(text);
  114.     text= new StyledText(fp, buf);
  115.     SafeDelete(buf);
  116.     textview->SetText(text);
  117.     textview->SetExtent(Point(textview->GetExtent().x, cFit));
  118.     return Dialog::ShowUnderMouse();
  119. }
  120.  
  121. void Alert::InspectorId(char *buf, int sz)
  122. {
  123.     if (textview)
  124.     textview->InspectorId(buf, sz);
  125.     else
  126.     Dialog::InspectorId(buf, sz);   
  127. }
  128.  
  129. static Alert *GetAlert(AlertType at)
  130. {
  131.     Alert *al= 0;
  132.  
  133.     if (Alert::alerts == 0)
  134.     Alert::alerts= new ObjArray((int)eAlertError);
  135.     
  136.     if ((al= (Alert*) Alert::alerts->At(at)) == 0) {
  137.     switch (at) {
  138.     case eAlertNote:
  139.         al= new Alert(at, (byte*) "note", gNote, "OK", cIdOk, 0);
  140.         break;
  141.     case eAlertCaution:
  142.         al= new Alert(at, (byte*) "Caution", gCaution,
  143.                         "Yes",    cIdYes,
  144.                         "No",     cIdNo,
  145.                         "Cancel", cIdCancel, 0);
  146.         break;
  147.     case eAlertStop:
  148.         al= new Alert(at, (byte*) "Stop", gStop,
  149.                      "Yes",    cIdYes,
  150.                      "No",     cIdNo,
  151.                      "Cancel", cIdCancel, 0);
  152.         break;
  153.     case eAlertSun:
  154.         al= new Alert(at, (byte*) "Message", gSun, "Ok", cIdOk, 0);
  155.         break;
  156.     case eAlertError:
  157.         if (Env::GetValue("Application.DebugButtonInAlert", FALSE)) 
  158.         al= new Alert(at, (byte*) "Error", gError,
  159.                       "Ignore",     cIdIgnore,
  160.                       "Dump Core",  cIdAbort,
  161.                       "Debug",      cIdInspect, 0);
  162.         else
  163.         al= new Alert(at, (byte*) "Error", gError,
  164.                       "Ignore",     cIdIgnore,
  165.                       "Dump Core",  cIdAbort, 0);
  166.         break;
  167.     case eAlertMessage:
  168.         al= new Alert(at, (byte*) "Message", 0, "Ok", cIdOk, 0);
  169.         break;
  170.     }
  171.     if (al)
  172.         Alert::alerts->AtPut(at, al);
  173.     }
  174.  
  175.     return al;
  176. }
  177.  
  178. int ShowAlert(AlertType at, char* va_(fmt), ...)
  179. {
  180.     int code= 0;
  181.     va_list ap;
  182.     Alert *al;
  183.     
  184.     va_start(ap, va_(fmt));
  185.     al= GetAlert(at);    
  186.     if (al)
  187.     code= al->ShowV(va_(fmt), ap);
  188.     va_end(ap);
  189.     return code;
  190. }
  191.  
  192. int ShowAlert(AlertType at, Font *fp, char* va_(fmt), ...)
  193. {
  194.     int code= 0;
  195.     va_list ap;
  196.     Alert *al;
  197.     
  198.     va_start(ap, va_(fmt));
  199.     al= GetAlert(at);    
  200.     if (al)
  201.     code= al->ShowV(fp, va_(fmt), ap);
  202.     va_end(ap);
  203.     return code;
  204. }
  205.  
  206.